﻿using DotNetExtensions.Core.Logging;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace DotNetExtensions.Core.MVC
{
    public class AppSettingDefault : Singleton<AppSettingDefault>
    {
        public JObject AppSetings { get; set; }
        private ILogger<AppSettingDefault> logger;
        private AppSettingDefault()
        {
            try
            {
                logger = LoggerManager.GetLogger<AppSettingDefault>();
                string filename = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "appsettings.json");
#if DEBUG
                string develop = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "appsettings.Development.json");
                if (File.Exists(develop)) filename = develop;
#endif
                if (File.Exists(filename))
                {
                    string json = File.ReadAllText(filename);

                    AppSetings = JObject.Parse(json);
                }
            }
            catch (Exception ex)
            {
                logger.LogError("读取配置文件失败，" + ex.Message + "，" + ex.StackTrace);
            }
        }
        private bool TryGet<T>(string key, out T value)
        {
            value = default(T);
            try
            {
                if (string.IsNullOrEmpty(key)) return false;
                if (Default.AppSetings != null)
                {
                    string[] keys = key.Split('.');
                    JToken token = Default.AppSetings[keys[0]];
                    for (int i = 1; i < keys.Length; i++)
                    {
                        token = token[keys[i]];
                    }
                    value = token.ToObject<T>();
                    return true;
                }
            }
            catch (Exception ex)
            {
                logger.LogError("读取配置[" + key + "]失败，" + ex.Message + "，" + ex.StackTrace);
            }
            return false;
        }
        public static bool TryGetValue<T>(string key, out T value)
        {
            return Default.TryGet(key, out value);
        }
        public T Get<T>(string key)
        {
            try
            {
                T value = default(T);
                if (string.IsNullOrEmpty(key)) return value;
                if (Default.AppSetings != null)
                {
                    string[] keys = key.Split('.');
                    JToken token = Default.AppSetings[keys[0]];
                    for (int i = 1; i < keys.Length; i++)
                    {
                        token = token[keys[i]];
                    }
                    value = token.ToObject<T>();
                }
                return value;
            }
            catch (Exception ex)
            {
                logger.LogError("读取配置[" + key + "]失败，" + ex.Message + "，" + ex.StackTrace);
            }
            return default(T);
        }
        public static T GetValue<T>(string key)
        {
            return Default.Get<T>(key);
        }
    }
}
